<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">考试管理</el-breadcrumb-item>
      <el-breadcrumb-item>考试列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="创建时间">
              <span>{{ props.row.createTime }}</span>
            </el-form-item>
            <el-form-item label="更新时间">
              <span>{{ props.row.updateTime }}</span>
            </el-form-item>
            <el-form-item label="考试开始时间">
              <span>{{ props.row.startTime }}</span>
            </el-form-item>
            <el-form-item label="考试结束时间">
              <span>{{ props.row.endTime }}</span>
            </el-form-item>
            <el-form-item label="报名开始时间">
              <span>{{ props.row.applyStartTime }}</span>
            </el-form-item>
            <el-form-item label="报名结束时间">
              <span>{{ props.row.applyEndTime }}</span>
            </el-form-item>
            <el-form-item label="创建人">
              <span>{{ props.row.userId }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="title" label="考试名称" width="200" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="description" label="简介" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="announcements" label="注意事项" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="price" label="考试费用" width="100" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
<!--scope封装表单信息-->
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="showEditFormDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
<!--修改时弹出的框-->
    <el-dialog title="修改相册信息" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" label-width="120px" style="width: 500px;margin: 0 auto">
        <el-form-item label="考试名称" >
          <el-input v-model="editForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="考试描述" >
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="报名开始时间">
          <div class="block">
            <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="editForm.startTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="报名结束时间">
          <div class="block">
            <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="editForm.endTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="考试费用">
          <el-input v-model="editForm.price"></el-input>
        </el-form-item>
        <el-form-item label="考试开始时间">
          <div class="block">
            <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="editForm.applyStartTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="考试结束时间">
          <div class="block">
            <el-date-picker
                value-format="yyyy-MM-dd HH:mm:ss"
                v-model="editForm.applyEndTime"
                type="datetime"
                placeholder="选择日期时间">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="注意事项">
          <el-input v-model="editForm.announcements"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 100px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  padding-left: 110px;
  margin-right: 10px;
  margin-bottom: 0;
  width: 50%;
}
</style>
<script>
export default {
  data() {
    return {
      tableData: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      editForm: {
        title: '',
        description: '',
        startTime: '',
        endTime: '',
        price: '',
        applyStartTime:'',
        applyEndTime: '',
        announcements: '',
      },
      formLabelWidth: '80px'
    }
  },
  methods: {
    showEditFormDialog(exam) {
      let url = 'http://120.77.154.166:8080/exam/' + exam.id;
      console.log('showEditFormDialog:url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        console.log(response.data)
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://120.77.154.166:8080/exam/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改相册成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadAlbumList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAlbumList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },

    openDeleteConfirm(exam) {
      console.log(exam)
      let message = '此操作将永久删除【' + exam.title + '】相册，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(exam);
      }).catch(() => {
      });
    },
    //向后端发送删除请求
    handleDelete(exam) {
      let url = 'http://120.77.154.166:8080/exam/' + exam.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除【' + exam.name + '】相册成功！',
            type: 'success'
          });
          this.loadAlbumList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAlbumList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },

    loadAlbumList() {
      let url = "http://120.77.154.166:8080/exam/list";
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
        }
      });
    }
  },
  mounted() {
    this.loadAlbumList();
  }
}
</script>
